// pages/userinfo/userinfo.js

//获取应用实例
const app = getApp()

Page({
  data: {
    userInfo: {},
    hasUserInfo: false,
    canIUseGetUserProfile: false,
  },

  onLoad: function () {
    // 查看是否授权
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }
  },

  getUserProfile(e) {
    wx.getUserProfile({
      desc: '用于完善会员资料',
      success: (res) => {
        console.log(res);
        if (res.userInfo.gender == 0) {
          app.gender = "未知";
        }
        if (res.userInfo.gender == 1) {
          app.gender = "男";
        }
        if (res.userInfo.gender == 2) {
          app.gender = "女";
        }
        app.avatarUrl = res.userInfo.avatarUrl;
        // console.log(app.avatarUrl);
        app.nickName = res.userInfo.nickName;
        app.sessionkey = wx.getStorageSync('sessionkey');
        app.openid = wx.getStorageSync('openid');
        app.globalData.userInfo = res.userInfo;
        wx.request({
          url: app.d.hostUrl + '/api/user/set_userinfo',
          data: {
            'avatarUrl': app.avatarUrl,
            'nickName': app.nickName,
            'gender': app.gender,
            'sessionkey': app.sessionkey,
            'openid': app.openid,
          },
          method: 'post', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
          header: {
            'Content-type': 'application/x-www-form-urlencoded'
          }, // 设置请求的 header
          success: function (res) {
            wx.showToast({
              title: '授权成功',
              duration: 2000
            });
            var userinfo = wx.getStorageSync('userinfo');
            if (userinfo.mobile == null){
              
              setTimeout(function () {
                wx.navigateTo({
                  url: '/pages/onepage/onepage'
                })
              }, 2000)
            }else{
              setTimeout(function () {
                wx.navigateTo({
                  url: '/pages/index/index', // 希望跳转过去的页面
                })
              }, 2000)
            }
          },
          fail: function (err) {
            console.log(err);
          }
        })
      }
    })
  },

  onShow: function () {
    
  },

  // 拨打电话
  opentel: function () {
    app.gotel();
  },

  /**
 * 用户点击右上角分享
 */
  onShareAppMessage: function () {
    var openid = wx.getStorageSync('openid');
    from: menu;
    return {
      title: '走吧商城',
      path: '/pages/onepage/onepage?openid=' + openid,
      success: function (res) {
        // console.log(openid);      
      },
      fail: function (res) {
        // console.log(res);
      }
    }
  },


})

app.avatarUrl = "";
app.nickName = "";
app.gender = "";
app.sessionkey = '';
app.openid = '';